/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.graph
 * author:David
 * Date:2015年8月21日上午11:36:20
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.graph;

import com.xuwei.base.link.Node;

/**
 * 边
 * @author David
 * @since 2015年8月21日 上午11:36:20
 * @version 
 * @since JDK 1.6
 */
public class Edge {

	public static final int NORMAL=0;
	public static final int MST=1;//MST边
	/*关键路径中的边*/
	private static final int CRITICAL=2;
	/*权值*/
	private int weight;
	/*边的信息*/
	private Object info;
	/*边在边表中的位置*/
	private Node edgePosition;
	/*边的第一顶点与第二顶点*/
	private Node firstVexPosition;
	/*在顶点表中的位置*/
	private Node secondVexPosition;
	/*边在第一（二）顶点的邻接（逆邻接）边表中的位置*/
	private Node edgeFirstPosition;
	/*在无向图中就是在两个顶点的邻接边表中的位置*/
	private Node edgeSecondPosition;
	/*边的类型*/
	private int type;
	/*所在图的类型*/
	private int graphType;
	
	//构建默认权值为1的边
	public Edge(Graph g,Vertex u,Vertex v,Object info) {
		this(g,u,v,info,1);
	}
	
	//构建边
	public Edge(Graph g,Vertex u,Vertex v,Object info,int weight) {
		 this.info=info;
		 this.weight=weight;
		 edgePosition=g.insert(this);
		 firstVexPosition=u.getVexPosition();
		 secondVexPosition=v.getVexPosition();
		 type=Edge.NORMAL;
		 graphType=g.getType();
		 if(graphType==Graph.UndirectedGraph){
			 //如果是无向图，边应当加入其两个顶点的邻接边表
			 edgeFirstPosition=u.getAdjacentEdges().insertLast(this);
			 edgeSecondPosition=v.getAdjacentEdges().insertLast(this);
		 }else{
			 //如果是有向图，边加入起始点的邻接边表，终止点的逆邻接边表
			 edgeFirstPosition=u.getAdjacentEdges().insertLast(this);
			 edgeSecondPosition=v.getReAdjacentEdges().insertLast(this);
		 }
	}

	//get&set methods
	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public Object getInfo() {
		return info;
	}

	public void setInfo(Object info) {
		this.info = info;
	}

	public Node getEdgePosition() {
		return edgePosition;
	}

	public void setEdgePosition(Node edgePosition) {
		this.edgePosition = edgePosition;
	}

	public Node getFirstVexPosition() {
		return firstVexPosition;
	}

	public void setFirstVexPosition(Node firstVexPosition) {
		this.firstVexPosition = firstVexPosition;
	}

	public Node getSecondVexPosition() {
		return secondVexPosition;
	}

	public void setSecondVexPosition(Node secondVexPosition) {
		this.secondVexPosition = secondVexPosition;
	}
	
	public Vertex getFirstVex(){
		return (Vertex) firstVexPosition.getData();
	}
	
	public Vertex getSecondVex(){
		return (Vertex) secondVexPosition.getData();
	}
	
	//与边的类型相关的方法
	/**
	 * 最小生成树的边
	 */
	public void setToMST(){
		type=Edge.MST;
	}
	public void setToCritical(){
		type=Edge.CRITICAL;
	}
	/**
	 * 普通的边
	 */
	public void resetType(){
		type=Edge.NORMAL;
	}
	public boolean isMSTEdge(){
		return type==Edge.MST;
	}
	public boolean isCritical(){
		return type==Edge.CRITICAL;
	}

	@Override
	public String toString() {
		return getInfo().toString().concat("[").concat(String.valueOf(getWeight())).concat("]");
	}

}
